package com.sc.VenusQueryBeta.ui.fragments.account.xiugai;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hengjun.kingstar.sdk.base.BasePresenter;
import com.hengjun.kingstar.sdk.base.fragment.BaseMVPCompatFragment;
import com.hengjun.kingstar.sdk.utils.ScreenUtils;
import com.hengjun.kingstar.sdk.utils.SpUtils;
import com.hengjun.kingstar.sdk.utils.ToastUtils;
import com.sc.VenusQueryBeta.R;
import com.sc.VenusQueryBeta.constant.Constants;
import com.sc.VenusQueryBeta.contract.account.xiugai.XiuGaiContract;
import com.sc.VenusQueryBeta.global.MyApplication;
import com.sc.VenusQueryBeta.model.bean.login.QueryRegisBean;
import com.sc.VenusQueryBeta.model.bean.login.UsersLoginBean;
import com.sc.VenusQueryBeta.presenter.account.xiugai.XiuGaiPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 账户页面的修改密码
 */
public class XiuGaiFragment extends BaseMVPCompatFragment<XiuGaiContract.XiuGaiPresenter> implements XiuGaiContract.IXiuGaiView {


    private static XiuGaiFragment fragment;
    @BindView(R.id.tv_hukou_oldpwd)
    EditText tvHukouOldpwd;
    @BindView(R.id.tv_hukou_newpwd)
    EditText tvHukouNewpwd;
    @BindView(R.id.tv_comfirm_hukoupwd)
    EditText tvComfirmHukoupwd;
    @BindView(R.id.btn_login_change_pwd)
    Button btnLoginChangePwd;
    @BindView(R.id.about_vip_user_back)
    ImageButton aboutVipUserBack;
    @BindView(R.id.vip_user)
    TextView vipUser;
    @BindView(R.id.rl_top)
    RelativeLayout rlTop;
    private QueryRegisBean queryRegisBean;
    private int clientindex;
    private UsersLoginBean loginBean;

    public static XiuGaiFragment newInstance(QueryRegisBean queryRegisBean, int clientindex) {
        Bundle args = new Bundle();
        args.putInt(Constants.CLIENTINDEX, clientindex);
        args.putSerializable(Constants.QUERY_SETTING, queryRegisBean);
        fragment = new XiuGaiFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_xiu_gai;
    }

    @Override
    public boolean onBackPressedSupport() {
        pop();
        return true;
    }

    /**
     * 修改密码
     */
    @Override
    public void initUI(View view, @Nullable Bundle savedInstanceState) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(rlTop.getLayoutParams());

        lp.setMargins(0, ScreenUtils.getStatusBarHeight(mContext)+8, 0, 0);

        rlTop.setLayoutParams(lp);
    }

    @NonNull
    @Override
    public BasePresenter initPresenter() {
        return XiuGaiPresenter.newInstance();
    }


    //初始化数据
    @Override
    public void initData() {
        super.initData();
        loginBean = SpUtils.getObject(MyApplication.app, UsersLoginBean.class);
        if (fragment.getArguments() != null) {
            queryRegisBean = (QueryRegisBean) fragment.getArguments().getSerializable(Constants.QUERY_SETTING);
            clientindex = fragment.getArguments().getInt(Constants.CLIENTINDEX, 0);
        }
    }

    @Override
    public void showNetworkError(Throwable throwable) {
        hideProgressDialog();
        ToastUtils.showToast("網絡出問題" + throwable.getMessage());
    }

    @Override
    public void userchangepwd(String userChangePwdBean) {
        if (userChangePwdBean.equals("200")) {
            hideProgressDialog();
//            SpUtils.clear(MyApplication.app);//
            ToastUtils.showToast("修改成功");
            pop();
            //   start(VipUserFragment.newInstance());
        } else if (userChangePwdBean.equals("-1")) {
            hideProgressDialog();
            ToastUtils.showToast("用戶不存在");
        } else if (userChangePwdBean.equals("-2")) {
            hideProgressDialog();
            ToastUtils.showToast("旧密碼錯誤");
        } else {
            hideProgressDialog();
            ToastUtils.showToast("未知錯誤");
        }

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
//        if (queryRegisBean != null && !queryRegisBean.getServerSetting().getServerAddress().equals("")) {
//            //查询所有厅
//            mPresenter.userchangepwd(clientindex,tvHukouOldpwd.getText().toString(),tvHukouNewpwd.getText().toString() ,queryRegisBean.getServerSetting().getServerAddress(), queryRegisBean.getServerSetting().getServerProject(), queryRegisBean.getServerSetting().getServerPort());
//
//        } else {
//            ToastUtils.showToast("数据异常");
//        }
    }

    @OnClick({R.id.about_vip_user_back, R.id.btn_login_change_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.about_vip_user_back:
                pop();
                break;
            case R.id.btn_login_change_pwd:
                if (!tvHukouNewpwd.getText().toString().trim().equals("") && !tvComfirmHukoupwd.getText().toString().trim().equals("")) {
                    if (tvHukouNewpwd.getText().toString().equals(tvComfirmHukoupwd.getText().toString())) {
                        showProgressDialog("正在修改");
                        mPresenter.userchangepwd(clientindex, tvHukouOldpwd.getText().toString(), tvHukouNewpwd.getText().toString(), queryRegisBean.getServerSetting().getServerAddress(), queryRegisBean.getServerSetting().getServerProject(), queryRegisBean.getServerSetting().getServerPort());
                    } else {

                        ToastUtils.showToast("两次输入的新密码不一致");
                    }
                } else {
                    ToastUtils.showToast("密码不能为空");
                }
                break;
        }
    }

}
